/**
 * 时间转换
 * @param time
 * @param cFormat
 * @returns {string|null}
 */
function parseTime(time, cFormat) {
    if (arguments.length === 0) {
        return null
    }
    const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
    let date
    if (typeof time === 'object') {
        date = time
    } else {
        if (('' + time).length === 10) time = parseInt(time) * 1000
        date = new Date(time)
    }
    const formatObj = {
        y: date.getFullYear(),
        m: date.getMonth() + 1,
        d: date.getDate(),
        h: date.getHours(),
        i: date.getMinutes(),
        s: date.getSeconds(),
        a: date.getDay()
    }
    const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
        let value = formatObj[key]
        if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1]
        if (result.length > 0 && value < 10) {
            value = '0' + value
        }
        return value || 0
    })
    return time_str
}

/**
 * 格式化时间
 * @param time
 * @param option
 * @returns {string}
 */
function formatTime(time, option) {
    time = +time * 1000
    const d = new Date(time)
    const now = Date.now()

    const diff = (now - d) / 1000

    if (diff < 30) {
        return '刚刚'
    } else if (diff < 3600) { // less 1 hour
        return Math.ceil(diff / 60) + '分钟前'
    } else if (diff < 3600 * 24) {
        return Math.ceil(diff / 3600) + '小时前'
    } else if (diff < 3600 * 24 * 2) {
        return '1天前'
    }
    if (option) {
        return parseTime(time, option)
    } else {
        return d.getMonth() + 1 + '月' + d.getDate() + '日' + d.getHours() + '时' + d.getMinutes() + '分'
    }
}
/**
 * @param {Number} timeStamp 判断时间戳格式是否是毫秒
 * @returns {Boolean}
 */
const isMillisecond = timeStamp => {
    const timeStr = String(timeStamp)
    return timeStr.length > 10
}

/**
 * @param {Number} timeStamp 传入的时间戳
 * @param {Number} currentTime 当前时间时间戳
 * @returns {Boolean} 传入的时间戳是否早于当前时间戳
 */
const isEarly = (timeStamp, currentTime) => {
    return timeStamp < currentTime
}

/**
 * @param {Number} num 数值
 * @returns {String} 处理后的字符串
 * @description 如果传入的数值小于10，即位数只有1位，则在前面补充0
 */
const getHandledValue = num => {
    return num < 10 ? '0' + num : num
}

/**
 * @param {Number} timeStamp 传入的时间戳
 * @param {Number} startType 要返回的时间字符串的格式类型，传入'year'则返回年开头的完整时间
 */
const getDate = (timeStamp, startType) => {
    const d = new Date(timeStamp * 1000)
    const year = d.getFullYear()
    const month = getHandledValue(d.getMonth() + 1)
    const date = getHandledValue(d.getDate())
    const hours = getHandledValue(d.getHours())
    const minutes = getHandledValue(d.getMinutes())
    const second = getHandledValue(d.getSeconds())
    let resStr = ''
    if (startType === 'year') {
        resStr = year + '-' + month + '-' + date + ' ' + hours + ':' + minutes + ':' + second
    } else {
        resStr = month + '-' + date + ' ' + hours + ':' + minutes
    }
    return resStr
}

/**
 * @param {String|Number} timeStamp 时间戳
 * @returns {String} 相对时间字符串
 */
const getRelativeTime = timeStamp => {
    // 判断当前传入的时间戳是秒格式还是毫秒
    const IS_MILLISECOND = isMillisecond(timeStamp)
    // 如果是毫秒格式则转为秒格式
    if (IS_MILLISECOND) {
        Math.floor(timeStamp /= 1000)
    }
    // 传入的时间戳可以是数值或字符串类型，这里统一转为数值类型
    timeStamp = Number(timeStamp)
    // 获取当前时间时间戳
    const currentTime = Math.floor(Date.parse(new Date()) / 1000)
    // 判断传入时间戳是否早于当前时间戳
    const IS_EARLY = isEarly(timeStamp, currentTime)
    // 获取两个时间戳差值
    let diff = currentTime - timeStamp
    // 如果IS_EARLY为false则差值取反
    if (!IS_EARLY) diff = -diff
    let resStr = ''
    const dirStr = IS_EARLY ? '前' : '后'

    if (diff <= 59) { // 少于等于59秒
        resStr = diff + '秒' + dirStr
    } else if (diff > 59 && diff <= 3599) { // 多于59秒，少于等于59分钟59秒
        resStr = Math.floor(diff / 60) + '分钟' + dirStr
    } else if (diff > 3599 && diff <= 86399) { // 多于59分钟59秒，少于等于23小时59分钟59秒
        resStr = getDate(timeStamp)
    //     resStr = Math.floor(diff / 3600) + '小时' + dirStr
    // } else if (diff > 86399 && diff <= 2623859) { // 多于23小时59分钟59秒，少于等于29天59分钟59秒
    //     resStr = Math.floor(diff / 86400) + '天' + dirStr
    // } else if (diff > 2623859 && diff <= 31567859 && IS_EARLY) { //多于29天59分钟59秒，少于364天23小时59分钟59秒，且传入的时间戳早于当前
    //     resStr = getDate(timeStamp)
    } else {
        resStr = getDate(timeStamp, 'year')
    }
    return resStr
}
/**
 * 时间戳转日期格式
 * @param timeStamp
 * @returns {string}
 */
const getLocalTime = (timeStamp) => {
    return new Date(parseInt(timeStamp) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');
}

/**
 * 格式化时间
 * @param date
 * @returns {string}
 */
const timeFormat = (date) =>{
    date = new Date(date)
    let year = date.getFullYear();
    const month = getHandledValue(date.getMonth() + 1)
    const day = getHandledValue(date.getDate())
    const hours = getHandledValue(date.getHours())
    const minutes = getHandledValue(date.getMinutes())
    const second = getHandledValue(date.getSeconds())
    return year + "/" + month + "/" + day +" " + hours + ":" + minutes + ":" + second;
}

module.exports = {
    parseTime,formatTime,getRelativeTime,getDate,timeFormat
}
